<?php

namespace app\admin\controller;

class Coupons extends Admin
{
    public function __construct()
    {
        parent::__construct();
        $this->model = 'Coupons';
        $this->assign("model", $this->model);
    }

    public function activity() {
        $getData = input('param.');
		$whereData = 'type=activity';
		if ($getData) {
			foreach ($getData as $k => $v) {
				if (!$whereData) {
					$whereData .= $k . '=' . $v;
				} else {
					$whereData .= '&' . $k . '=' . $v;
				}
			}
		}
		$this->assign('keywords', isset($getData['keywords']) ? $getData['keywords'] : '');
		$this->assign("whereData", $whereData);
		return $this->fetch();
    }

    public function editactivity() 
    {
        $couponsid = input("param.couponsid/s");
        trace($couponsid);
        //
        $info = model('Coupons')->where('id', $couponsid)->find();
		$this->assign("info", $info);
		return $this->fetch();
    }

    public function get_where_map()
    {
        $whereData = input("param.whereData/s");
        $whereData = urldecode($whereData);
        $whereDatas = [];
        $whereDatas['type'] = ['neq', 'activity'];
        if ($whereData) {
            $whereData = explode('&', $whereData);
            foreach ($whereData as $v) {
                $r = explode('=', $v);
                if ($r[0] == 'keywords') {
                    $whereDatas['id|name'] = ['like', '%' . $r[1] . '%'];
                } else {
                    $whereDatas[$r[0]] = $r[1];
                }
            }
        }
        $roleid = $this->uinfo['roleid'];
        $userid = $this->uinfo['userid'];
        if ($roleid == 1) {
            
        } else if($roleid == 4) {
            $whereDatas['createrid'] = ['neq', '1'];
        } else {
            $whereDatas['createrid'] = $userid;
        }
        return $whereDatas;
    }

    public function edit() {
		$items = input("param.items");
		$items = trim($items, ",");
		$model = input("param.model");
		if (!$model) {
			$model = $this->model;
		}
		$db = model($model);
		$info = $db->get_data(['item_id' => $items]);
        trace($info);
        $info['type'] = $info['type'] ? config('COUPONS_TYPE.' . $info['type']) : '未设置';
        $info['canceltype'] = $info['canceltype'] ? config('HEXIAO_TYPE.' . $info['canceltype']) : '未设置';
		$this->assign("info", $info);
		return $this->fetch();
	}

    public function avaiable()
    {
        return $this->fetch();
    }

    public function set_dealer()
    {
        $postData = input("post.postData");
        parse_str($postData, $dataarr);
        $couponsid = $dataarr['couponsid'];
        $userids = [];
        if (isset($dataarr['check_userids'])) {
            $userids = $dataarr['check_userids'];
        }
        if ($couponsid) {
            model('CouponsUser')->where('couponsid', $couponsid)->delete();
        }
        $data = [];
        foreach ($userids as $userid) {
            $a_data['couponsid'] = $couponsid;
            $a_data['userid'] = $userid;
            $data[] = $a_data;
            $a_data = [];
        }
        if ($data) {
            $id = model('CouponsUser')->save_data($data);
            if ($id === false) {
                return json(['status' => 0, 'msg' => '保存失败']);
            }
        }
        return json(['status' => 1, 'msg' => '操作成功']);
    }

    public function choose_dealer()
    {
        $where = [];
        if ($this->uinfo['roleid'] == 1 || $this->uinfo['roleid'] == 4) {
            $where['roleid'] = 6;
        }
        $list = model('AdminUser')->where($where)->select();
        $this->assign('lists', $list);
        // 
        $couponsid = input("param.couponsid/s");
        if($couponsid != null) {
            $ids = model('CouponsUser')->where('couponsid', $couponsid)->field('userid')->select();
            $checked_userids = '';
            foreach ($ids as $value) {
                $checked_userids = $checked_userids . $value['userid'] . ',';
            }
            if ($checked_userids != '') {
                $checked_userids = rtrim($checked_userids, ',');
            }
            trace($checked_userids);
            $this->assign('couponsid', $couponsid);
            $this->assign('checked_userids', $checked_userids);
        }
        return $this->fetch();
    }

    public function data_save() {
		$model = input("post.model");
		if (!$model) {
			$model = $this->model;
		}
		$db = model($model);
		$postData = input("post.postData");
		parse_str($postData, $dataarr);
        // trace('exit');exit();
        $dataarr['createrid'] = $this->uinfo['userid'];
        $dataarr['opentime'] = strtotime($dataarr['opentime']);
        $dataarr['closetime'] = strtotime($dataarr['closetime']);
        $dataarr['starttime'] = strtotime($dataarr['starttime']);
        $dataarr['endtime'] = strtotime($dataarr['endtime']);
        // if ($this->uinfo['roleid'] == 4) {
        //     $dataarr['auditstatus'] = 1;
        // }
		$id = $db->save_data($dataarr);
		if ($id === false) {
			return json(['status' => 0, 'msg' => $db->getError()]);
		}
		return json(['status' => 1, 'msg' => '操作成功']);
	}

    // 给历史会员补发优惠券
    public function sendcoupons() {
        $c_id = input("param.items");
        $status = model('Coupons')->where('id', $c_id)->column('status');
        if ($status[0]) {
            exec_php('/index/cron/send_coupons', ['type' => 'send_again', 'couponsid' => $c_id]);
            return json(['status' => 1, 'msg' => '操作成功']);
        } else {
            return json(['status' => 0, 'msg' => '该优惠券已经失效']);
        }
    }

    // 区域管理员通过审核
    public function audit() {
        $c_id = input("param.items");
        $roleid = $this->uinfo['roleid'];
        if ($roleid == 1 || $roleid == 4) { // 只有超级管理员和区域管理员才有审核权限
            model('Coupons')->where('id', $c_id)->setField('auditstatus', '1');
            return json(['status' => 1, 'msg' => '操作成功']);
        } else {
            return json(['status' => 0, 'msg' => '该账号没有审核权限']);
        }
    }

     // 区域管理员驳回审核
     public function rejectaudit() {
        $c_id = input("param.items");
        $roleid = $this->uinfo['roleid'];
        if ($roleid == 1 || $roleid == 4) { // 只有超级管理员和区域管理员才有审核权限
            model('Coupons')->where('id', $c_id)->setField('auditstatus', '2');
            return json(['status' => 1, 'msg' => '操作成功']);
        } else {
            return json(['status' => 0, 'msg' => '该账号没有审核权限']);
        }
    }

    public function add_activity() {
        return $this->fetch();
    }

    // 修改该优惠券的状态
    public function setstatus() {
        $postData = input("post.");
        $couponid = $postData['id'];
        $status = $postData['value'];
        model('Coupons')->where('id', $couponid)->setField('status', $status);
    }

     // 修改该优惠券的审核状态
     public function setauditstatus() {
        $postData = input("post.");
        $couponid = $postData['id'];
        $auditstatus = $postData['value'];
        trace('qqqqq');
        trace($auditstatus);
        model('Coupons')->where('id', $couponid)->setField('auditstatus', $auditstatus);
        return json(['status' => 1, 'msg' => '操作成功']);
    }
}
 